package com.jxt.myjxtmanager.ui.presenter;

import android.content.Context;

import com.jxt.myjxtmanager.db.SaleUser;
import com.jxt.myjxtmanager.ui.inter.LoginInter;
import com.jxt.myjxtmanager.ui.model.LoginMo;
import com.jxt.myjxtmanager.utils.LogUtils.LogInfo;


/**
 * Created by Administrator on 2016/1/20 0020.
 */
public class LoginPresenter implements LoginMo.OnloginResultListener {
    private LoginMo loginMo;
    private LoginInter loginInter;
    private Context context;

    public LoginPresenter(Context context, LoginInter loginInter) {
        this.context = context;
        this.loginInter = loginInter;
        loginMo = new LoginMo(context);
    }

    /**
     * 登陆逻辑
     */
    public void toLogin() {
        loginMo.setUsername(loginInter.getUsername());
        loginMo.setPassword(loginInter.getPassword());
        if (!loginMo.MoValidate()) {
            loginInter.showToast("用户名或密码不能为空");
        } else if (loginInter.getUsername().length() != 11) {
            loginInter.showToast("用户名必须是11位的手机号");
        } else {//验证成功，调用登录接口登录
            loginMo.httpLogin(this);
        }
    }

    /**
     * 登陆成功 返回成功接口回调
     *
     * @param saleUser
     */
    @Override
    public void onSuccess(SaleUser saleUser) {
        loginInter.skipToMainAc();
    }


    /**
     * 登陆失败  返回接口回调
     *
     * @param code 返回code标记
     * @param msg  失败信息
     */
    @Override
    public void onFailure(int code, String msg) {
        LogInfo.d("--------------------失败code： " + code);
        if (code == 2) {
            LogInfo.d("--------------------login get token error");
            loginInter.showToast(msg);
        } else {
            loginInter.showToast("登录失败");
        }
    }
}
